<?php

namespace App\Http\Controllers\Home\Login;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;
use Crypt;
use session;
class LoginController extends Controller
{
    //前台首页
    public function login()
    {	
    	//页面跳转
    	return view('home.login.login');
    }

    //前台用户登录信息
    public function dologin(Request $request)
    {
    	$data = $request -> except('_token');
    	$email = $data['loginemail'];
    	//与数据库匹配
    	$res = DB::table('users') -> where('email', $email) ->first();

    	if(!$res)
    	{
    		return back() -> with(['info' => '邮箱错误']);
    	}

    	//取出密码
    	$password = $res -> password;
    	//解密
    	$repassword = Crypt::decrypt($password);

    	if($repassword != $data['loginpassword'])
    	{
    		return back() -> with(['info' => '密码错误']);
    	}

    	foreach ($res as $key => $value) {
    		$info[$key] = $value;
    	}

    	//把数据存入session
    	session(['master' => $info]);
    	return redirect('home/index');

    }

    //用户注销
    public function quit()
    {
        session()->forget('master');
        return redirect('/home/index');
    }
}
